

/**
 * 网格数据插值
 * @param {*} ratio 0-1之间
 * @param {*} gridData0 第一个gridData数据
 * @param {*} gridData1 第二个gridData数据
 * @param {*} length 数组长度
 * @param {*} resultData 结果数据
 * @returns {number[] | undefined} 返回结果，如果为undefined，说明插值出错！
 * @static
 * @memberof Obj.HeatMap
 * @example
 * var d0 = [10, 20, 30, 40]
 * var d1 = [20, 30, 40, 50];
 * var r = new Array(4); r.fill(0);
 * r = XE.Obj.HeatMap.interpolateGridData(0.4, d0, d1, 4, r);
 */
function interpolateGridData2(ratio, gridData0, gridData1, length, resultData) {
  try {
    if (!resultData) {
      return undefined;
    }

    for (var i = 0; i < length; ++i) {
      resultData[i] = gridData0[i] * (1.0 - ratio) + gridData1[i] * ratio;
    }

    return resultData;
  } catch (error) {
    return undefined;
  }
}

/**
 * 网格数据插值
 * @param {*} ratio 0-1之间
 * @param {*} gridDataArr gridData数组
 * @param {*} length 数组长度
 * @param {*} resultData 结果数据
 * @returns {number[] | undefined} 返回结果，如果为undefined，说明插值出错！
 * @static
 * @memberof Obj.HeatMap
 * @example
 * var d0 = [10, 20, 30, 40]
 * var d1 = [20, 30, 40, 50];
 * var r = new Array(4); r.fill(0);
 * r = XE.Obj.HeatMap.interpolateGridData(0.4, [d0, d1], 4, r);
 */
function interpolateGridData(ratio, gridDataArr, length, resultData) {
  try {
    if (!resultData) {
      return undefined;
    }

    const nGrid = gridDataArr.length;
    let r = ratio*nGrid;
    let k = Math.floor(r)
    r -= k;

    const g0 = gridDataArr[k]
    const g1 = nGrid>1?gridDataArr[k+1]:gridDataArr[k];

    for (var i = 0; i < length; ++i) {
      resultData[i] = g0[i] * (1.0 - r) + g1[i] * r;
    }

    return resultData;
  } catch (error) {
    return undefined;
  }
}

export default interpolateGridData;
